home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 051-075 / scopedisk69 / cclat / src / dstr.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  2KB  |  101 lines

  1. /*
  2.  * dynamic strings
  3.  * March 1989, Miles Bader
  4.  */
  5.  
  6. #include "common.h"
  7. #include "dstr.h"
  8.  
  9. struct dstr *dstr_Create(initsize)
  10. int initsize;
  11. {
  12.     struct dstr *n=NEW(struct dstr);
  13.     
  14.     DBUG_ENTER("dstr_Create");
  15.  
  16.     if(n==NULL)
  17.        fatal("couldn't allocate a dstr");
  18.  
  19.     n->buf=malloc(initsize+1);
  20.  
  21.     if(n->buf==NULL)
  22.        fatal("couldn't allocate dstr of size %d",initsize);
  23.  
  24.     n->max=initsize;
  25.     n->len=0;
  26.     *n->buf='\0';
  27.  
  28.     DBUG_RETURN(struct dstr *,n);
  29. }
  30.  
  31. void dstr_Free(ds)
  32. struct dstr *ds;
  33. {
  34.     DBUG_ENTER("dstr_Free");
  35.  
  36.     if(ds->buf!=NULL)
  37.        FREE(ds->buf);
  38.     FREE(ds);
  39.  
  40.     DBUG_VOID_RETURN;
  41. }
  42.  
  43. void dstr_Clear(ds)
  44. struct dstr *ds;
  45. {
  46.    ds->len=0;
  47.    *ds->buf='\0';
  48. }
  49.  
  50. void dstr_Append(ds,str)
  51. struct dstr *ds;
  52. char *str;
  53. {
  54.     int len=strlen(str);
  55.     
  56.     DBUG_ENTER("dstr_Append");
  57.  
  58.     if(len+ds->len>ds->max){
  59.        ds->max+=len+len;
  60.        ds->buf=(char *)realloc((void *)ds->buf,ds->max);
  61.  
  62.        if(ds->buf==NULL)
  63.            fatal("couldn't extend a dstr to %d chars",ds->max);
  64.     }
  65.  
  66.     strcpy(ds->buf+ds->len,str);
  67.     ds->len+=len;
  68.  
  69.     DBUG_VOID_RETURN;
  70. }
  71.  
  72. void dstr_Set(ds,str)
  73. struct dstr *ds;
  74. char *str;
  75. {
  76.     DBUG_ENTER("dstr_Set");
  77.  
  78.     ds->len=0;
  79.     dstr_Append(ds,str);
  80.  
  81.     DBUG_VOID_RETURN;
  82. }
  83.  
  84. void dstr_Addf(ds,fmt,a1,a2,a3,a4,a5,a6,a7)
  85. struct dstr *ds;
  86. char *fmt;
  87. char *a1,*a2,*a3,*a4,*a5,*a6,*a7;
  88. {
  89.     char buf[500];
  90.  
  91.     DBUG_ENTER("dstr_Addf");
  92.  
  93.     sprintf(buf,fmt,a1,a2,a3,a4,a5,a6,a7);
  94.  
  95.     DBUG_2("buf",buf);
  96.  
  97.     dstr_Append(ds,buf);
  98.  
  99.     DBUG_VOID_RETURN;
  100. }
  101.